<?php 

class MemberPresenter extends BasePresenter{
	
	private $model;
	/**
	 * Start lifecycle presenter
	 *
	 * @return void
	 */
	protected function startup(){
		parent::startup();
		
		$this->model = MemberModel::getInstance();
	}

	/**
	 * Settings for all render
	 *
	 * @return void
	 */
	protected function beforeRender(){

	}
	
	public function getMessages(){
		
		return $this->messages;
	}
	
	public function boxHTML($id){
		$this->model = MemberModel::getInstance();
		
		$this->session = Environment::getSession("web");
		
		// pokud je pozadavek na prihlaseni, overime uzivatele a prihlasime pripadne
		if(isset($_POST["login"])){
			$auth = $this->model->authenticateMember($_POST["login"], $_POST["password"]);

			if($auth) $this->session->member = $auth;
			else $this->messages[] = array("message" => "Přihlášení se nezdařilo. Zkontrolujte zadané údaje.", "error" => "error");
		}

		if(isset($_POST["odhlasit"]) || $_GET["odhlasit"]){
			unset($this->session->member);
		}
		
		$link = $this->link("Member:", array("id"=>$id));
		
		$template = new Template();
		$template->registerFilter(new LatteFilter);
		$template->aktualni = $_SERVER["REQUEST_URI"];
		$template->link = $link;
		$template->member = $this->session->member;
		$template->setFile(APP_DIR."/modules/Member/templates/front/box.phtml");
		
		return $template;
	}
	
	public function boxHTML2($id){
		
		$link = $this->link("Member:", array("id"=>$id));
		
		$template = new Template();
		$template->registerFilter(new LatteFilter);
		$template->aktualni = $_SERVER["REQUEST_URI"];
		$template->link = $link;
		$template->member = $this->session->member;
		$template->setFile(APP_DIR."/modules/Member/templates/front/box2.phtml");
		
		return $template;
	}
	
	/**
	 * Render default action
	 *
	 * @return void
	 */
	public function renderDefault($id){

		if($this->presenter->getParam("akce") == "registrace"){
			
			$address = $this->getPresenter()->link("Member:", array("id" => $id, "akce" => "registrace"));
					
			$navigace = array(
								"address" => $address,
								"name" => "Registrace"
								);
			
			$this->addToNavigation($navigace);
			
			if(isset($_POST["registrace"])){
				
				if(!empty($_POST["jmeno"]) && !empty($_POST["nick"]) && !empty($_POST["email"])
				&& !empty($_POST["password"]) && !empty($_POST["heslo_potvrdit"]) && $_POST["password"] == $_POST["heslo_potvrdit"]){

					unset($_POST["registrace"]);
					unset($_POST["heslo_potvrdit"]);
					
					$_POST["password"] = sha1($_POST["password"]);
					
					$ok = $this->model->addMember($_POST);
					if($ok) $this->flashMessage("Vaše registrace proběhla úspěšně. Nyní se můžete přihlásit za pomoci vaší přezdívky a hesla.");
				}else{
					
					$this->flashMessage("Vyplňte prosím správně všechny údaje.");
				}
				
			}
			
			$this->template->akce = "registrace";
		}
		elseif($this->presenter->getParam("akce") == "detail"){
			
			$id = $this->presenter->getParam("ident");
			
			$address = $this->getPresenter()->link("Member:", array("id" => $id, "akce" => "detail", "ident" => $id));
					
			$navigace = array(
								"address" => $address,
								"name" => "Detail"
								);
			
			$this->addToNavigation($navigace);
			
			$member = $this->model->fetchMembers(array("id_member" => $id));
			$member[0]["picture"] = $this->model->getPicture($member[0]["id_file"]);
			$this->template->member = $member[0];
			$this->template->akce = "detail";
		}
		else{
			
			$this->template->members = $this->model->fetchMembers(array("clen" => 1));
			$this->template->akce = "vypis";
		}
		
		$this->template->aktualni = $_SERVER["REQUEST_URI"];
		$this->template->id = $id;
	}
	
}

?>
